package com.mmlc.meimei.lichengkongjian.mine;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.inter.ViewPagerFragmentAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.MyCouponRequestFilter;
import com.mmlc.network.responsebean.MyCouponResponseBean;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 优惠券
 * by king
 * Created at 2016/8/30 16:44
 */
public class DiscountCouponActivity extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_discount_coupon;
    }

    /**
     * 0:default;1:order form
     */
    int type = 0;

    @Override
    protected void initExtra() {
        super.initExtra();
        type = getIntent().getIntExtra("type", 0);
    }

    List<RadioButton> radios = new ArrayList<>();
    List<Fragment> fragments = new ArrayList<>();
    RadioButton rb_unused, rb_overdue, rb_used;

    ViewPager vp_coupon;

    @Override
    protected void initView() {
        rb_unused = ViewUtils.getView(activity, R.id.rb_unused);
        rb_overdue = ViewUtils.getView(activity, R.id.rb_overdue);
        rb_used = ViewUtils.getView(activity, R.id.rb_used);
        radios.add(rb_unused);
        radios.add(rb_overdue);
        radios.add(rb_used);

        vp_coupon = ViewUtils.getView(activity, R.id.vp_coupon);
        fragments.add(new DiscountCouponFragment());
        fragments.add(new DiscountCouponFragment());
        fragments.add(new DiscountCouponFragment());
        for (int i = 0; i < fragments.size(); i++) {
            Bundle bundle = new Bundle();
            bundle.putInt("index", i);
            bundle.putInt("type", type);
            fragments.get(i).setArguments(bundle);
        }
        vp_coupon.setOffscreenPageLimit(3);
        vp_coupon.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragments));
    }

    @Override
    protected void initInfo() {
        MyCouponRequestFilter filter = new MyCouponRequestFilter(activity);
        filter.requestBean.type = String.valueOf(0);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<MyCouponResponseBean>() {
            @Override
            public void onSuccess(MyCouponResponseBean result) {
                if (result == null || result.getData() == null) return;
                if (!ViewUtils.isEmpty(result.getData().getUnuse_num()))
                    rb_unused.setText("未使用 (" + result.getData().getUnuse_num() + ")");
                if (!ViewUtils.isEmpty(result.getData().getExpire_num()))
                    rb_overdue.setText("已过期 (" + result.getData().getExpire_num() + ")");
                if (!ViewUtils.isEmpty(result.getData().getUse_num()))
                    rb_used.setText("已使用 (" + result.getData().getUse_num() + ")");
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
            }
        });
    }

    int navIndex = 0;

    @Override
    protected void initEvent() {
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);

        ((RadioGroup) ViewUtils.getView(activity, R.id.rg_group)).setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.rb_unused:
                                navIndex = 0;
                                break;
                            case R.id.rb_overdue:
                                navIndex = 1;
                                break;
                            case R.id.rb_used:
                                navIndex = 2;
                                break;
                        }
                        vp_coupon.setCurrentItem(navIndex, false);
                    }
                }
        );

        vp_coupon.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                navIndex = position;
                radios.get(navIndex).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
